package com.muqi.app.qlearn.login;

import java.util.List;

import com.easemob.EMValueCallBack;
import com.muqi.app.im.qmain.EMHelper;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.MenuActivity;
import com.muqi.app.qlearn.student.R;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

public class SkipActivity extends BaseFragmentActivity {
	/** 加载闪屏界面 */
	private ImageView skipLayout;
	private boolean isLogin = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_skipping);
	}

	@Override
	protected void onInit() {
		skipLayout = (ImageView) findViewById(R.id.skip_layout);
		skipLayout.setImageResource(R.drawable.skiping_for_student);

	}

	@Override
	protected void onStart() {
		AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
		animation.setFillAfter(true);
		animation.setDuration(1000);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				if (isLogin) { // 为了防止 没有登录应该进入登录界面，但是这个操作是后台的操作，仍然会开始主界面
					startMenuAcitivity();
				}
			}
		});
		skipLayout.startAnimation(animation);
		super.onStart();

	}

	@Override
	protected void onData() {
		/*if (NetWorkUtils.isNetworkAvailable(this)) {
			if (TextUtils.isEmpty(mSpUtil.getToken())) {
				isLogin = false;
				startLoginAcitivity();
			} else {
				if (EMHelper.getInstance().isLoggedIn()) {
					refreshHxConfigs();
				} else {
					isLogin = false;
					startLoginAcitivity();
				}
			}
		} else {
			ShowToast.showShort(this, R.string.net_break);
			startLoginAcitivity();
		}*/
		startMenuAcitivity();
	}

	private void refreshHxConfigs() {
		// 注册群组和联系人监听
		EMHelper.getInstance().registerGroupAndContactListener();
		EMHelper.getInstance().asyncFetchContactsFromServer(
				new EMValueCallBack<List<String>>() {

					@Override
					public void onSuccess(List<String> arg0) {
						runOnUiThread(new Runnable() {
							public void run() {

							}
						});
					}

					@Override
					public void onError(int arg0, String arg1) {
						runOnUiThread(new Runnable() {
							public void run() {

							}
						});
					}
				});
	}

	/**
	 * 跳转登录界面
	 */
	public void startLoginAcitivity() {
		startActivity(LoginActivity.class);
		SkipActivity.this.finish();
	}

	/**
	 * 跳转完善信息界面
	 */
	public void startCustomerAcitivity() {
		new Handler().postDelayed(new Runnable() {
			public void run() {
				// startActivity(CustomerActivity.class);
				SkipActivity.this.finish();
			}
		}, 1000);
	}

	/**
	 * 跳转到主界面
	 */
	public void startMenuAcitivity() {
		new Handler().postDelayed(new Runnable() {
			public void run() {
				startActivity(MenuActivity.class);
				SkipActivity.this.finish();
			}
		}, 1000);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		isLogin = false;
		// skipLayout.clearAnimation();
	}
}
